package com.security.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @author 顾志杰
 * @date 2020/6/27-10:11
 */
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private UserDetailsServiceImpl userDetailsService;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .formLogin()
                /*登录页面*/
                .loginPage("/login")
                /*登录认证处理*/
                .loginProcessingUrl("/login")
                /*登录成功后跳转到什么页面*/
                .defaultSuccessUrl("/home")
                /*登录失败跳转*/
                .failureUrl("/login")
                .permitAll()
                .and()
                /*退出*/
                .logout()
                .permitAll()
                .and()
                .authorizeRequests()
                /*不需要登录认证就可以访问的url*/
                .antMatchers("/").permitAll()
                .antMatchers("/home").authenticated()
//                /*访问资源和角色*/
//                /*角色*/
//                .antMatchers("/hello").hasAnyAuthority("ROLE_admin")
//                /*角色*/
//                .antMatchers("/hello2").hasAnyRole("admin")
//                /*资源角色id*/
//                .antMatchers("/hello2").hasAuthority("sys:admin")
                /*所有请求必须在登录后才能进行*/
//                .anyRequest().authenticated()
                .anyRequest().access("@rbacService.hasPermssion(request,authentication)")
                .and()
                .sessionManagement()
                /*session超时后跳转的页面*/
                .invalidSessionUrl("/login");
    }

    //    @Override
//    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//        auth.inMemoryAuthentication()
//                .withUser("user")
//                .password(passwordEncoder.encode("123456"))
//                .roles("user")
//                .and()
//                .withUser("admin")
//                .password(passwordEncoder.encode("123456"))
//                .roles("admin")
//                .authorities("sys:admin")
//                .and()
//                .passwordEncoder(passwordEncoder);
//    }
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService)
                .passwordEncoder(passwordEncoder);
    }


}
